<?php
/**
 * @package     Joomla.Platform
 * @subpackage  Form
 *
 * @copyright   Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE
 */

defined('JPATH_PLATFORM') or die;

jimport('joomla.html.html');
jimport('joomla.form.formfield');
jimport('joomla.form.helper');
//JFormHelper::loadFieldClass('list');

/**
 * Form Field class for the Joomla Platform.
 * Supports an HTML select list of categories
 *
 * @package     Joomla.Platform
 * @subpackage  Form
 * @since       11.1
 */
class JFormFieldAlbumData extends JFormField
{
	/**
	 * The form field type.
	 *
	 * @var    string
	 * @since  11.1
	 */
	public $type = 'AlbumData';

	/**
	 * Method to get the field options for category
	 * Use the extension attribute in a form to specify the.specific extension for
	 * which categories should be displayed.
	 * Use the show_root attribute to specify whether to show the global category root in the list.
	 *
	 * @return  array    The field option objects.
	 * @since   11.1
	 */
	protected function getInput() {
		if (empty($this->value))
			$data = array(array('full'=>'','thumbnail'=>'','caption'=>''));
		else
			$data = $this->value;
		$name = $this->name;

		$full_label = JText::_('PLG_CONTENT_XPROP_ALBUM_DATA_FULL_LABEL');
		$thumbnail_label = JText::_('PLG_CONTENT_XPROP_ALBUM_DATA_THUMBNAIL_LABEL');
		$caption_label = JText::_('PLG_CONTENT_XPROP_ALBUM_DATA_CAPTION_LABEL');

		foreach($data as $i=>$datum) {		
			$full = $name."[$i][full]";
			$thumbnail = $name."[$i][thumbnail]";
			$caption = $name."[$i][caption]";
			if (is_object($datum)){
				$full_value = $datum->full;
				$thumbnail_value = $datum->thumbnail;
				$caption_value = $datum->caption;
			} else {
				$full_value = $datum['full'];
				$thumbnail_value = $datum['thumbnail'];
				$caption_value = $datum['caption'];
			}
			$textbox_width = 40;
			// FOR LOOP HERE
			$html.= <<<EOD
<div class="row">
	<p><label>$full_label</label><input type="text" class="full" name="$full" value="$full_value" size="$textbox_width" />
	</p>
	<p><label>$thumbnail_label:</label><input type="text" class="thumbnail" name="$thumbnail" value="$thumbnail_value" size="$textbox_width" />
	</p>
	<p><label>$caption_label:</label><textarea class="caption" name="$caption" cols="30">$caption_value</textarea>
	</p>
	<p><a href="#" class="albumdata-grid-remove">Remove</a></p>
</div>
EOD;
		}
		$button = '<div style="clear:both"><a href="#" id="albumdata-grid-add">Add</a></div>';
		
		$html = '<div name="'.$this->name.'" class="grid" id="albumdata-grid">'.$html.'</div>'.$button;
		
		$script = <<<EOD
   \n
	window.addEvent('domready', function(){
		var grid = $('albumdata-grid');
		grid.getElements('.albumdata-grid-remove').addEvent('click', function(e){
			e.stop();
			this.getParent('.row').destroy().empty();
			renameRows();
		});
		grid.getElement('#albumdata-grid-add').addEvent('click',function(e){
			e.stop();
			
			var row = grid.getLast('.row');
			var cloned = row.clone();
			cloned.getElement('.full').set('value','');	
			cloned.getElement('.thumbnail').set('value','');
			cloned.getElement('.caption').set('value','');

			cloned.inject(row,'after');
			renameRows();
			return false;
		});
		
		function renameRows(){
			// Rename row elements
			var rows = $$('.row');
			var rootName = grid.get('name');
			for (var i=0;i<rows.length;i++){
				rows[i].getElement('.full').set('name',rootName + '[' + i + '][full]');	
				rows[i].getElement('.thumbnail').set('name',rootName + '[' + i + '][thumb]');
				rows[i].getElement('.caption').set('name',rootName + '[' + i + '][caption]');
			}
		}
	});
EOD;
		$style = <<<EOD
   .albumdata-grid-remove, #albumdata-grid-add{float: right}
   .row:only-child .albumdata-grid-remove{display:none}
   .row {float: left; border-bottom:1px solid #ccc; width: 100%}
   #albumdata-grid .row label {padding-left:10px;margin-right: -10px}
EOD;
		
		JFactory::getDocument()->addScriptDeclaration($script);
		JFactory::getDocument()->addStyleDeclaration($style);
		return $html;
	}
}
